<?php
require_once BACKGROUND_COMMON_ACTION_SERVICE_DIR . '/default.php';

class background_good_category_update_action_service extends background_default_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {
        //var_dump(__METHOD__); exit();
                
        $result = array('status' => 0, 'message' => '', 'data' => array());
	     
	    $id        = init_user_data_key($_POST, 'id', 'int');
	    $parent_id = init_user_data_key($_POST, 'parent_id', 'int');
	    $name      = init_user_data_key($_POST, 'name', 'str');
	    $order     = init_user_data_key($_POST, 'order', 'int');
	    $status    = init_user_data_key($_POST, 'status', 'int');
	     
	    if(empty($id)){
	        $result['status']  = -6;
	        $result['message'] = '请选择分类';
	    }else if(empty($name)){
	        $result['status']  = -1;
	        $result['message'] = '请输入分类名称';
	    }else if((empty($parent_id) && $parent_id !== 0) || $parent_id < 0){
	        $result['status']  = -2;
	        $result['message'] = '请选择上级分类';
	    }else if((empty($order) && $order !== 0) || $order < 0){
	        $result['status']  = -3;
	        $result['message'] = '请输入排序';
	    }else if((empty($status) && $status !== 0) || $status < 0){
	        $result['status']  = -4;
	        $result['message'] = '请选择状态';
	    }else{
	        $model_background_good_category = wwy_resource_container::get_model_background_good_category();
	        
	        $good_category_info = $model_background_good_category->find($id);
	        if(empty($good_category_info)){
	            $result['status']  = -7;
	            $result['message'] = '该分类不存在';
	        }else{
	            //【父分类】不能是【当前分类】及其【子分类】
	            $except_parent_id_list = $model_background_good_category->get_except_parent_id_list($id);
	            if(in_array($parent_id, $except_parent_id_list)){
	                $result['status']  = -8;
	                $result['message'] = '不正确的上级';
	            }else{
	             
    	            $good_category_info   = array(
    	                'id' => $id,
    	                'parent_id' => $parent_id,
    	                'name' => $name,
    	                'order' => $order,
    	                'status' => $status,
    	            );
    	            
    	            $update_time = time();
    	            $good_category_info['update_time'] = isset($_POST['update_time']) ? intval($_POST['update_time']) : $update_time;
    	            
    	            
    	            $write_db = $this->_get_write_db();
    	            $pdo = $write_db->get_connection();
    	            $pdo->beginTransaction();
    	            
    	            try{
    	                $row_count = $model_background_good_category->update($good_category_info);
    	            
    	                if($row_count === false){
    	                    $result['status']  = -5;
    	                    $result['message'] = '修改失败';    	                   
    	                }else{
    	                    $result['status']  = 1;
    	                    $result['message'] = '修改成功';
    	                    
    	                    wwy_resource_container::get_cache_handler()->flush();
    	                     
    	                    $pdo->commit();
    	                }    	                    	                
    	            }catch (Exception $e){
    	                $pdo->rollBack();
    	            
    	                throw $e;
    	            }
	            }
	        }	        
	    }
	    
	    self::set_view_status($result['status']);
	    self::set_view_message($result['message']);
    }
}